package com.renyi.model;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.dao.DataAccessException;


/**
 * Mybatis 数据库操作实现类 MybatisRepositoryImpl.java Create on 2014-4-29
 * @author dasuan
 */
public class MybatisRepositoryDoctorImpl implements MybatisRepositoryDoctor{
	private SqlSessionTemplate sqlSessionTemplateDoctor;

	public MybatisRepositoryDoctorImpl() {
	};

	public MybatisRepositoryDoctorImpl(SqlSessionTemplate sqlSessionTemplateDoctor) {
		this.sqlSessionTemplateDoctor = sqlSessionTemplateDoctor;
	}

	/**
	 * 查询记录
	 * @author dasuan
	 * @version 1.0
	 * @param querySqlName ,执行的语句名称
	 * @return List<?>
	 */
	public List<HashMap<String, String>> findMybatisList(String statement) {
		return this.sqlSessionTemplateDoctor.selectList(statement);
	}

	/**
	 * 查询记录
	 * @author dasuan
	 * @version 1.0
	 * @param querySqlName ,执行的语句名称
	 * @param paramsMap ,参数Map
	 * @return List<Object>
	 */
	public List<String> findMybatisString(String statement, Map<String, Object> parameter) {
		return this.sqlSessionTemplateDoctor.selectList(statement, parameter);
	}
	
	/**
	 * 查询记录总数
	 * 
	 * @param querySqlName ,执行的语句名称
	 * @param paramsMap ,参数Map
	 * @return Long,记录数
	 */
	public Long findIbatisListCount(String querySqlName, Map<String, Object> paramsMap) throws DataAccessException { 
		return this.sqlSessionTemplateDoctor.selectOne(querySqlName, paramsMap);
	}

	/**
	 * 插入记录
	 * 
	 * @param insertqlName ,执行的语句名称
	 * @param paramsMap ,参数Map
	 * @return int,影响记录数
	 */
	public Object insertIbatisObject(String insertSqlName, Map<String, Object> paramsMap) throws DataAccessException {
		return this.sqlSessionTemplateDoctor.insert(insertSqlName, paramsMap);
	}
	/**
	 * 插入记录
	 * 
	 * @param insertqlName ,执行的语句名称
	 * @param paramsMap ,参数Map
	 * @return int,影响记录数
	 */
	public Object insertIbatisObject(String insertSqlName, Object object) throws DataAccessException {
		return this.sqlSessionTemplateDoctor.insert(insertSqlName, object);
	}
	
	/**
	 * 批量插入记录
	 * 
	 * @param insertqlName ,执行的语句名称
	 * @param paramsMap ,参数Map
	 * @return int,影响记录数 
	 */
	public Object batchIbatisObject(String insertSqlName,  List<Map<String, Object>> paramsMapList) throws DataAccessException {
		int lastKey =0;//最后的ID
		try { 
			lastKey = this.sqlSessionTemplateDoctor.insert(insertSqlName, paramsMapList);
	        return lastKey;
		 }catch(Exception e){
			// e.printStackTrace();
	    	return 0; 
		 } 
	}
	
	/**
	 * 更新记录
	 * 
	 * @param updateSqlName ,执行的语句名称
	 * @param paramsMap ,参数Map
	 * @return int,影响记录数
	 */
	public int updateIbatisObject(String updateSqlName, Map<String, Object> paramsMap) throws DataAccessException {
		return this.sqlSessionTemplateDoctor.update(updateSqlName, paramsMap);
	}

	/**
	 * 删除记录
	 * 
	 * @param deleteSqlName ,执行的语句名称
	 * @param paramsMap ,参数Map
	 * @return int,影响记录数
	 */
	public int deleteIbatisObject(String deleteSqlName, Map<String, Object> paramsMap) throws DataAccessException { 
		return this.sqlSessionTemplateDoctor.delete(deleteSqlName, paramsMap);
	}
	
	
	public List<HashMap<String, Object>> procedureMybatisList(String statement,
			Map<String, Object> paramsMap) {
		// TODO Auto-generated method stub
		return this.sqlSessionTemplateDoctor.selectList(statement, paramsMap);
	}

	public List<HashMap<String, Object>> findMybatisList(String statement,
			Map<String, Object> paramsMap) {
		// TODO Auto-generated method stub
		return this.sqlSessionTemplateDoctor.selectList(statement, paramsMap);
	}

	public List<HashMap<String, Object>> getMybatisList(String statement,
			Map<String, Object> paramsMap) {
		// TODO Auto-generated method stub
		return this.sqlSessionTemplateDoctor.selectList(statement, paramsMap);
	}
}
